
var EventType = {
    SCROLL_START : 0,
    SCROLL_ING :1,
    SCROLL_END :2
}

cc.Class({
    extends: cc.Component,

    properties: {
        content:cc.Node,
        deltaX : 100, //x间隔距离
        centerScale :1.0,
        selectEvents : {
            "default": [],
            type: cc.Component.EventHandler,
            tooltip: "i18n:COMPONENT.button.click_events"
        }
    },
    statics :{
        EventType: EventType
    },

    onLoad(){
        this.testTime = 0.2
        this.childs = [];
        for(var i=0; i<this.content.children.length; i++){
            this.childs[i] = this.content.children[i];
            this.childs[i].x = this.deltaX*(i-1);
        }
        this.isTouching = false
        this.hasTouchMove = false

        this.isTestX = false;
        this.currentIndex = 0;
    },
    init(idx){
        this.currentIndex = idx;
        for(var i=0; i<this.childs.length; i++){
            this._checkChildX(this.childs[i],(i-idx) *this.deltaX)
        }
    },
    scrollTo(idx){
        this.isTestX = true;
        this.testTime = 0.2

        this._toMoveX =(idx-this.currentIndex)*this.deltaX
    },
    

    _checkChildX(child,x){
        if(x > this.childs.length/2 * this.deltaX){
            x-=this.childs.length * this.deltaX
        } else if(x < -this.childs.length/2 * this.deltaX){
            x+=this.childs.length * this.deltaX
        }
        child.x = x;
        if(Math.abs(x) < this.deltaX){
            child.scale = (1 - Math.abs(x)/this.deltaX)*(this.centerScale-1)+1
        } else {
            child.scale = 1;
        }
    },

    start(){
        this.content.on(cc.Node.EventType.TOUCH_START,this._onTouch,this);
        this.content.on(cc.Node.EventType.TOUCH_MOVE,this._onTouch,this);
        this.content.on(cc.Node.EventType.TOUCH_END,this._onTouchEnd,this);
        this.content.on(cc.Node.EventType.TOUCH_CANCEL,this._onTouchEnd,this);
    },
    _onTouch(event){
        if(event.type == cc.Node.EventType.TOUCH_START){
            this.isTouching = true;
            this.hasTouchMove = false
            this.dx = event.getStartLocation().x;
            var event = {
                target :this,
                type :EventType.SCROLL_START,
                index : this.currentIndex
            }
            cc.Component.EventHandler.emitEvents(this.selectEvents, event);
            return;
        }
        this.hasTouchMove = true;
        var dx = event.getLocation().x - this.dx
        this._move(dx);
        this.dx = event.getLocation().x
    },
    _onTouchEnd(event){
        this.isTouching = false;
        if(!this.hasTouchMove){
            if(this.isTestX){
                return;
            }
            var p = this.content.convertToNodeSpaceAR(event.getLocation())
            // this.scrollTo(this.currentIndex+(p.x>0?1:-1))

            this._toMoveX = (p.x>0?1:-1) * this.deltaX
            this.isTestX = true
            return;
        }
        
        var x = this.childs[0].x;
        var idx = Math.round(x/this.deltaX);
        var tox = this.deltaX * idx;
        this._toMoveX = tox - x;
        this.isTestX = true
    },
    _move(dt){
        for(var i=0; i<this.childs.length; i++){
            this._checkChildX(this.childs[i],this.childs[i].x+dt)
        }
    },
    update(dt){
        if(this.isTouching || !this.isTestX){
            return;
        }

        var stepx = this._toMoveX * dt * 5

        for(var i=0; i<this.childs.length; i++){
            this._checkChildX(this.childs[i],this.childs[i].x+stepx)
        }

        var x = this.childs[0].x;
        var idx = Math.round(x/this.deltaX);
        var tox = this.deltaX * idx;
        var finish = false;
        if(this.hasTouchMove){
            finish = Math.abs(tox-x) <= Math.abs(stepx)
        } else {
            this.testTime -= dt;
            finish = this.testTime <=0
        }
        if(finish){
            this.isTestX = false;
            var idx = this.childs.length;
            var minx = this.deltaX*2;
            // this.currentIndex = this.childs.findIndex(item=>{
            //     return Math.abs(item.x)<=Math.abs(stepx)
            // })

            for(var i=0; i<this.childs.length; i++){
                this._checkChildX(this.childs[i],this.childs[i].x+tox-x)
                if(Math.abs(this.childs[i].x) < Math.abs(minx)){
                    minx = this.childs[i].x
                    idx = i
                }
            }

            this.currentIndex=idx;
            var event = {
                target :this,
                type :EventType.SCROLL_END,
                index : this.currentIndex
            }
            this.hasTouchMove = false;
            cc.Component.EventHandler.emitEvents(this.selectEvents, event);
        }
    }

});
